package com.jsgt.greekfire.ui.pool.transfercoin

import android.text.InputType
import android.text.TextUtils
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.PriceFormatUtil
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.dialog.IDialog
import com.jsgt.greekfire.dialog.MenuDialog
import com.jsgt.greekfire.event.AssetsEvent
import com.jsgt.greekfire.event.PoolEvent
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.factory.UserViewModelFactory
import com.jsgt.greekfire.common.util.NetDataManager
import com.jsgt.greekfire.viewmodel.CoinViewModel
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.activity_transfer_coin.*
import kotlinx.android.synthetic.main.activity_transfer_coin.confirm_tv


import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 转入转出
 * author fhx
 * create time 2020/4/23 18:08
 **/
class TransferCoinActivity : BaseActivity() {
    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(
            this,
            UserViewModelFactory()
        ).get(UserViewModel::class.java)
    }

    var coinType = "3"//3是utit,4ut
    var avaiBalance = 0.0
    private val dotNumber = 4
    override fun getLayoutId(): Int = R.layout.activity_transfer_coin

    override fun main() {


        back.setOnClickListener {
            finish()
        }
//
//        nav_rb_1.text = "选择币种"
//        nav_rb_2.text = "矿池"
//
//        radioGroup.setOnCheckedChangeListener { _, checkedId ->
//            run {
//                if (checkedId == transfer_1.id) {
//                    openpool_text1.text = "选择币种"
//                    openpool_text2.text = "矿池"
//
//                } else {
//                    openpool_text3.visibility = View.GONE
//                    openpool_text1.text = "矿池"
//                    openpool_text2.text = "GFC"
//                }
//            }
//        }

        transfer_num_et.requestFocus()
        transfer_num_et.numberDot = dotNumber
        NetDataManager.getUserAssets()
        confirm_tv.setOnClickListener {
            submit()
        }



        transfer_num_et.inputType = InputType.TYPE_CLASS_NUMBER
        transfer_num_et.inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL

        all_num_tv.setOnClickListener {
            transfer_num_et.setText(PriceFormatUtil.format(avaiBalance, dotNumber))
            transfer_num_et.setSelection(transfer_num_et.text!!.length)
        }
        transfer_cb.setOnCheckedChangeListener { _, isChecked ->
            updateView(isChecked)
            transfer_num_et.setText("")
        }
        coinViewModel.rollInOrOutLiveData.observe(this, Observer {
            showProgress(false)
            ToastUtils.show(this, "提交成功")
            EventBus.getDefault().post(PoolEvent())
            finish()
        })
        coinViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
        })

        cl_top.setOnClickListener {
            if (NetDataManager.findAssetsBean("4") != null) {
                val items = arrayListOf("UTIT", "UT")
                val dialog: MenuDialog = IDialog.getInstance().showItemDialog(
                    this, "选择币种", items,
                    arrayListOf(R.mipmap.icon_gft, R.mipmap.icon_glb)
                ) {
                    val tCoinType = if (it == 0) "3" else "4"
                    if (tCoinType != coinType) {
                        coinType = tCoinType
                        updateView(transfer_cb.isChecked)
                        NetDataManager.getUserAssets()
                    }
                } as MenuDialog
                dialog.setCanceledOnTouchOutside(true)
            }
        }

        updateView(transfer_cb.isChecked)
    }

    private fun updateView(check: Boolean) {
        //check true是从矿池往资产转
        if (check) {
            transfer_assets_tv.text = getString(R.string.account_pool)
            transfer_pool_tv.text = getString(R.string.account_assets)
            avaiBalance = NetDataManager.findAssetsBean(coinType)!!.assetFrozen!!
        } else {
            transfer_assets_tv.text = getString(R.string.account_assets)
            transfer_pool_tv.text = getString(R.string.account_pool)
            avaiBalance = NetDataManager.findAssetsBean(coinType)!!.assetBalance!!
        }
        tv_coin_type.text = NetDataManager.findAssetsBean(coinType)!!.assetName
        transfer_num_et.hint =
            "当前数量${PriceFormatUtil.format(
                avaiBalance,
                4
            )}"
    }

    private fun submit() {
        val number = transfer_num_et.text.toString().trim()
        if (TextUtils.isEmpty(number)) {
            ToastUtils.show(this, "请输入数量")
            return
        }
        if (!TextUtils.isEmpty(number) && number.toDouble() <= 0) {
            ToastUtils.show(this, "数量必须大于0")
            return
        }
        if (number.toDouble() > avaiBalance) {
            ToastUtils.show(this, "余额不足")
            return
        }
        showProgress(true)
        if (transfer_cb.isChecked) {
            coinViewModel.rollInOrOut(
                "1",
                number,
                coinType
            )
        } else {
            coinViewModel.rollInOrOut(
                "0",
                number,
                coinType
            )
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAssetsEvent(e: AssetsEvent) {
        updateView(transfer_cb.isChecked)
    }
}